<?php
namespace Store\Widget;

use Think\Controller;

class PingxxWidget extends Controller
{



    public function getParams($order_id){

        $order = D('Store/Order')->getById($order_id);
        $channel = I('post.channel');
        $aSubject = I('post.subject',modC('WEB_SITE_NAME', 'OpenSNS开源社交系统', 'Config').'微店在线支付');
        $aBody = I('post.body',         '通过'.get_pay_method($channel).'进行微店商品在线支付');
        if($order['condition'] == 1){
            $this->error('该订单已经支付');
        }

        if($order['uid'] != is_login()){
            $this->error('不是您的订单');
        }
        return array('channel'=>$channel,'body'=>$aBody,'subject'=>$aSubject,'amount'=>$this->getAmount($order));
    }


    public function getAmount($order){

        $finally =$order['total_cny'] + $order['adj_cny'];
         $score_id=modC('CURRENCY_TYPE',4,'Store');
        $fields_config = modC('RE_FIELD', "", 'Pingxx');
        $fields = json_decode($fields_config,true);
        $type = array_search_key($fields,'FIELD',$score_id);
        !$type && $type['UNIT'] =1;
        return number_format($finally/$type['UNIT'], 2, ".", "");
    }





    public function afterPay($event)
    {
        $charge = $event['data']['object'];
        $order_id = $charge['order_no'];
        if($charge['paid']){
            $data['condition'] = 1; //置为已支付
            $data['pay_time'] = $charge['time_paid'];
            $orderModel =  D('store_order');
            $s_uid = $orderModel->where(array('id'=>$order_id))->getField('s_uid');
            $shopModel = D('Store/StoreShop');
            $shopModel->where(array('uid' => $s_uid))->setInc('order_count', 1);
            $orderModel ->where(array('id'=>$order_id))->save($data);
            M('order_link') ->where(array('order_id'=>$order_id))->setField('method',$charge['channel']);
        }

        return '';
    }





}